package com.imti.mall.order.controller;

import com.imti.mall.common.Api.CommonPage;
import com.imti.mall.common.Api.CommonResult;
import com.imti.mall.model.order.pojo.OmsOrderReturnReason;
import com.imti.mall.model.order.query.ReturnResonApplyQuery;
import com.imti.mall.order.service.OmsOrderReturnReasonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@Api(tags = "退货原因管理")
@RequestMapping("/api/v1/orders/returnReason")
public class ReturnReasonController {

  @Autowired
  private OmsOrderReturnReasonService omsOrderReturnReasonService;

  @ApiOperation(value = "查询退货原因列表")
  @GetMapping
  public CommonResult list(ReturnResonApplyQuery returnResonApplyQuery,
                                                             @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                             @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
    List<OmsOrderReturnReason> omsOrderReturnReasonList = omsOrderReturnReasonService.findOmsOrderReturnReason(returnResonApplyQuery,pageSize,pageNum);
      return CommonResult.success(CommonPage.restPage(omsOrderReturnReasonList));
  }

  @ApiOperation(value = "查询退货原因详情")
  @GetMapping("/{id}")
  public CommonResult<OmsOrderReturnReason> getOmsOrderReturnResonById(@PathVariable Long id){
    OmsOrderReturnReason omsOrderReturnReason = omsOrderReturnReasonService.getById(id);
    return CommonResult.success(omsOrderReturnReason);
  }

  @ApiOperation(value = "删除对应id的原因")
  @PostMapping("/delete")
  public CommonResult deleteReson(@RequestParam("ids") List<Long> idList){
    boolean  result = omsOrderReturnReasonService.removeByIds(idList);
    if(result){
      return CommonResult.success(1);
    }else{
      return CommonResult.success(0);
    }
  }

  @ApiOperation(value = "修改原因")
  @PostMapping("/update")
  public CommonResult updateReson(@RequestBody OmsOrderReturnReason omsOrderReturnReason){
    boolean result = omsOrderReturnReasonService.updateById(omsOrderReturnReason);
    if(result){
      return CommonResult.success(1,"操作成功");
    }else{
      return CommonResult.success(0,"系统繁忙，请稍后重试");
    }
  }

  @ApiOperation(value = "新增原因")
  @PostMapping("/create")
  public CommonResult addReson(@RequestBody OmsOrderReturnReason omsOrderReturnReason){
    boolean result = omsOrderReturnReasonService.save(omsOrderReturnReason);
    if(result){
      return CommonResult.success(1);
    }else{
      return CommonResult.success(0);
    }
  }

  @ApiOperation(value = "修改原因状态")
  @PostMapping("/update/status")
  public CommonResult updateResonStatus(@RequestParam("status") Long status,@RequestParam("ids")Long ids){
    OmsOrderReturnReason omsOrderReturnReason = new OmsOrderReturnReason(ids,status);
    int result = omsOrderReturnReasonService.updateStatusById(omsOrderReturnReason);
    if(result>0){
      return CommonResult.success(1);
    }else{
      return CommonResult.failed("修改失败");
    }
  }
}
